package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcProduceService;
import com.vtuzx.kingdee.xqc_api.service.IXqcPurchaseService;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 生产入库
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/prd_in")
@RestController
public class XqcPrdInController {

    /** 生产服务 */
    @Autowired
    private IXqcProduceService produceService;

    /**
     * 保存生产入库单(到审核阶段)
     */
    @PostMapping("/save_user_prdin_by_prdcol_list")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "billNoList", label = "生产收货单号列表", notNull = true)
    } )
    public Object saveUserPrdInByPrdcolList(@VtuzxControlParam VtuzxToken token, @SuppressWarnings("unused") @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<String> billNoList = VtuzxUtil.getObject(param, "billNoList");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        produceService.saveUserPrdInByPrdColList(_userId, name, stockNo, locationNo, billNoList);
        return null;
    }

    /**
     * 保存生产入库单(到审核阶段)
     */
    @PostMapping("/save_user_prdin")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object saveUserPrdIn(@VtuzxControlParam VtuzxToken token, @SuppressWarnings("unused") @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        produceService.saveUserPrdIn(_userId, name, stockNo);
        return null;
    }

    /**
     * 添加用户临时生产入库物料箱号
     */
    @PostMapping("/add_user_prdin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "count", label = "数量", notNull = true, isNumber = true)
    } )
    public Object addUserPrdInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        double count = VtuzxUtil.getDouble(param, "count");
        return produceService.addUserPrdInMaterialBox(_userId, name, stockNo, locationNo, boxNo, count);
    }

    /**
     * 添加用户临时生产入库物料箱号
     */
    @PostMapping("/add_user_prdin_material_box_list")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "boxList", label = "箱号列表", notNull = true)
    } )
    public Object addUserPrdInMaterialBoxList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        List<Map<String, Object>> boxList = VtuzxUtil.getObject(param, "boxList");
        return produceService.addUserPrdInMaterialBoxList(_userId, name, stockNo, locationNo, boxList);
    }

    /**
     * 删除用户临时生产入库物料箱号
     */
    @PostMapping("/delete_user_prdin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true)
    } )
    public Object deleteUserPrdInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        return produceService.deleteUserPrdInMaterialBox(_userId, id);
    }

    /**
     * 删除用户临时生产入库物料
     */
    @PostMapping("/delete_user_prdin_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "id", notNull = true)
    } )
    public Object deleteUserPrdInMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        produceService.deleteUserPrdInMaterial(_userId, id);
        return null;
    }

    /**
     * 删除用户临时生产入库单(清空)
     */
    @PostMapping("/delete_user_prdin")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true)
    } )
    public Object deleteUserPrdIn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        produceService.deleteUserPrdIn(_userId, stockNo);
        return null;
    }

    /**
     * 编辑入库物料箱号重量
     */
    @PostMapping("/update_user_prdin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "count", label = "重量", notNull = true, isNumber = true)
    } )
    public Object updateUserPrdInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        double count = VtuzxUtil.getDouble(param, "count");
        return produceService.updateUserPrdInMaterialBox(_userId, boxNo, count);
    }

    /**
     * 获取用户的入库物料列表
     */
    @PostMapping("/get_user_prdin_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
    } )
    public Object getUserPrdInMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        return produceService.getUserPrdInMaterial(_userId, stockNo);
    }

    /**
     * 获取用户的入库物料箱号列表
     */
    @PostMapping("/get_user_prdin_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "id", label = "入库物料单据id", notNull = true)
    } )
    public Object getUserPrdInMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String id = VtuzxUtil.getString(param, "id");
        return produceService.getUserPrdInMaterialBox(_userId, id);
    }

    /**
     * 获取用户的生产入库单
     */
    @PostMapping("/get_prdin_bill")
    @VtuzxApi
    public Object getPrdInBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize, 10);
        return produceService.getPrdInBill(_userId, pageIndex, pageSize, param);
    }

    /**
     * 获取用户的生产入库单分录列表
     */
    @PostMapping("/get_prdin_bill_detail")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "stockInBillNo", label = "入库单号", notNull = true)
    } )
    public Object getPrdInBillDetail(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String stockInBillNo = VtuzxUtil.getString(param, "stockInBillNo");
        return produceService.getPrdInBillDetail(_userId, stockNo, stockInBillNo);
    }

    /**
     * 获取用户的生产入库单分录的箱号详细
     */
    @PostMapping("/get_prdin_bill_detail_box")
    @VtuzxApi
    public Object getPrdInBillDetailBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String _detailId = VtuzxUtil.getString(param, "_detailId");
        return produceService.getPrdInBillDetailBox(_userId, _detailId);
    }

    /**
     * 传金蝶退料
     */
    @PostMapping("/sync_kingdee_return")
    @VtuzxApi
    public Object syncKingdeeReturn(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = VtuzxUtil.getString(token,"userName");
        List<String> billNoList = VtuzxUtil.getObject(param, "billNoList");
        return produceService.syncKingdeeReturn(_userId, userName, billNoList);
    }

    /**
     * 传金蝶变品退料
     */
    @PostMapping("/sync_kingdee_desalting")
    @VtuzxApi
    public Object syncKingdeeDesalting(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = VtuzxUtil.getString(token,"name");
        List<Map<String,Object>> billNoList = VtuzxUtil.getObject(param, "billNoList");

        return produceService.syncKingdeeDesalting(_userId, userName, billNoList);
    }

    /**
     * 传金蝶入库
     */
    @PostMapping("/sync_kingdee_in_stock")
    @VtuzxApi
    public Object syncKingdeeInStock(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String userName = VtuzxUtil.getString(token,"name");
        List<String> billNoList = VtuzxUtil.getObject(param, "billNoList");

        return produceService.syncKingdeeInStock(_userId, userName, billNoList);
    }
}
